package com.yuanduan.mplus.presenter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.cj.framework.util.FileUtils;
import com.cj.framework.util.HttpImp;
import com.cj.framework.util.IHttp;
import com.cj.framework.widget.CircleImageView;
import com.yuanduan.mplus.R;
import com.yuanduan.mplus.config.Config;
import com.yuanduan.mplus.model.entity.MessageEntity;

import java.util.List;

/**
 * Created by cj on 2016/7/5.
 * Email:781806481@qq.com
 */
public class MessageAdapter extends BaseAdapter {
    private List<MessageEntity> datas;
    private Context context;
    private IHttp iHttp=new HttpImp();
    private Bitmap myHead;

    public MessageAdapter(Context context, List<MessageEntity> lists){
        this.context=context;
        datas=lists;
        myHead= FileUtils.getBitmapFromFile(Config.HEADLOCATION);
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MessageEntity entity= (MessageEntity) getItem(position);
        View view = null;
        if(entity.isMe()==true){
            view=LayoutInflater.from(context).inflate(R.layout.message_listview_me,null);
        }else{
            view=LayoutInflater.from(context).inflate(R.layout.message_listview_other,null);
        }
        TextView tv= (TextView) view.findViewById(R.id.message_listview_content);
        tv.setText(entity.getMessage());
        final CircleImageView circleImageView= (CircleImageView) view.findViewById(R.id.message_listview_head);
        if(entity.isMe()){
            circleImageView.setBitmap(myHead);
        }else{
            iHttp.httpBitmapGet(Config.URL + entity.getUserID(), new IHttp.HttpInfoCallback() {
                @Override
                public void onSucceed(Object result) {
                    circleImageView.setBitmap((Bitmap) result);
                }

                @Override
                public void onFailed(Exception e) {

                }
            });
        }
        return view;
    }
}
